Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alembic Setup #869

Merged
merged 17 commits into from
Oct 2, 2023
Merged

Alembic Setup #869

merged 17 commits into from
Oct 2, 2023

Conversation

nrjadkry
Copy link
Contributor

@nrjadkry nrjadkry commented Oct 2, 2023

Description:
This pull request introduces Alembic setup to our existing database configuration, enhancing our database management capabilities. Alembic is a powerful tool for database schema versioning and migration, and this addition will streamline the process of maintaining and evolving our database schema.

Changes Made:
Integration of Alembic into our project's database setup.
Configuration files for Alembic migrations.
Initial database migration scripts.

Benefits:
Improved database version control: Alembic allows us to track changes to our database schema over time, making it easier to roll back changes if necessary and maintain a consistent schema across different environments.
Simplified database migrations: Alembic automates the process of creating and applying database migrations, reducing the risk of human error during schema updates.
Enhanced collaboration: With Alembic in place, multiple team members can work on database changes concurrently, ensuring smoother collaboration and reducing conflicts.

Todo

  1. In order to fully operationalize Alembic, we may need to set up an entry point that allows executing the following command for running migration files:
    alembic upgrade head
  2. Allow access to create migration files in the versions folder. Currently, the setup does not permit users to create new files in the versions folder

@spwoodcock spwoodcock added priority:low Backlog of tasks that will be addressed in time backend Related to backend code in progress labels Oct 2, 2023
Copy link
Member

@spwoodcock spwoodcock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra tasks complete 👍

We don't need to change any permissions to the versions dir.
Migration files are generated during local development and committed to the repo, then they are available to the migration container at runtime.

@spwoodcock spwoodcock merged commit c8f4632 into development Oct 2, 2023
1 of 3 checks passed
@spwoodcock spwoodcock deleted the alembic branch October 2, 2023 19:37
@spwoodcock
Copy link
Member

spwoodcock commented Oct 2, 2023

Running alembic upgrade head seems to work.

I can't run alembic revision though.
Could you look into it @nrjadkry please?

Error:

NameError: name '_proxy' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/appuser/.local/bin/alembic", line 8, in <module>
    sys.exit(main())
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/config.py", line 630, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/config.py", line 624, in main
    self.run_cmd(cfg, options)
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/config.py", line 601, in run_cmd
    fn(
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/command.py", line 240, in revision
    scripts = [script for script in revision_context.generate_scripts()]
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/command.py", line 240, in <listcomp>
    scripts = [script for script in revision_context.generate_scripts()]
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/autogenerate/api.py", line 640, in generate_scripts
    yield self._to_script(generated_revision)
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/autogenerate/api.py", line 545, in _to_script
    return self.script_directory.generate_revision(
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/script/base.py", line 676, in generate_revision
    self.revision_map.get_revisions(head),
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/script/revision.py", line 526, in get_revisions
    resolved_id, branch_label = self._resolve_revision_number(
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/script/revision.py", line 750, in _resolve_revision_number
    self._revision_map
  File "/home/appuser/.local/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 1138, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/script/revision.py", line 191, in _revision_map
    for revision in self._generator():
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/script/base.py", line 148, in _load_revisions
    script = Script._from_filename(self, dir_name, filename)
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/script/base.py", line 1036, in _from_filename
    module = util.load_python_file(dir_, filename)
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 109, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/opt/migrations/versions/ec28a415c8d8_create_inital_tables.py", line 18, in <module>
    (os.getcwd() + "/" + op.get_context().script.dir + "/alembic_helpers.py"),
  File "<string>", line 7, in get_context
  File "/home/appuser/.local/lib/python3.10/site-packages/alembic/util/langhelpers.py", line 99, in _name_error
    raise NameError(
NameError: Can't invoke function 'get_context', as the proxy object has not yet been established for the Alembic 'Operations' class.  Try placing this code inside a callable.

To reproduce:

docker compose up -d api
docker compose exec api bash
alembic revision

A good thing to test would be removing: password = Column(String) from users as we no longer need it.

@nrjadkry
Copy link
Contributor Author

nrjadkry commented Oct 3, 2023

#873
should fix your issue of not being able to run alembic revision @spwoodcock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to backend code priority:low Backlog of tasks that will be addressed in time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants